﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GroupsServiceLibrary;
using AAttributeServiceLibrary1;
using AttributesGroupsServiceLibrary;
using UserServiceLibrary;
using UserGroupsServiceLibrary;

namespace NascarEventsWPF
{
    /// <summary>
    /// Interaction logic for Page3_UG_AG.xaml
    /// </summary>
    public partial class Page3_UG_AG : Window
    {
        //creates objects from service classes - Beth
        AttributeService1 atc = new AttributeService1();
        AttributeGroupsService1 agc = new AttributeGroupsService1();
        GroupsService1 gs = new GroupsService1();

        //Amha
        private UserServiceLibrary.UserService UserService = new UserServiceLibrary.UserService();
        private UserGroupsServiceLibrary.UserGroupsService UserGroupService = new UserGroupsServiceLibrary.UserGroupsService();

        public Page3_UG_AG()
        {
            InitializeComponent();
        }



        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            
            //fills datatables for attributes and groups for display
            DataTable attribTable = atc.FillDataTable("tbAttributes");
            DataTable groupTable = gs.FillDataTable("tbGroups");

            //fills listbox
            attributes_listbox.ItemsSource = attribTable.DefaultView;
            attributes_listbox.DisplayMemberPath = attribTable.Columns["attributeName"].ToString().Trim();

            //groups_listbox
            groups_listbox.ItemsSource = groupTable.DefaultView;
            groups_listbox.DisplayMemberPath = groupTable.Columns["groupName"].ToString().Trim();
            
            // fills user listbox
            LB_USER.ItemsSource = UserService.GetAllUser();
            LB_USER.DisplayMemberPath = "FirstName";

            // fills groups listbox
            LB_GROUPS.ItemsSource = groupTable.DefaultView;           
            LB_GROUPS.DisplayMemberPath = groupTable.Columns["groupName"].ToString().Trim();

        }

        
        //add attribute to group - from Beth's code
        private void AddAttribtoGroupbtn_Click(object sender, RoutedEventArgs e)
        {
            string selectedGroup = "";
            string selectedAttrb = "";

            //gets selection from group listbox
            foreach (var item in groups_listbox.SelectedItems)
            {
                selectedGroup = ((DataRowView)item)["GroupName"].ToString();
            }

            //gets selection from attribute listbox
            foreach (var item in attributes_listbox.SelectedItems)
            {
                selectedAttrb = ((DataRowView)item)["AttributeName"].ToString();
            }

            
            int gaValue = Convert.ToInt32(GA_Value_txt.Text);
            string agd = "N";

            if (disableNGA_rb.IsChecked == true)
            { agd = "N"; }
            else
            { agd = "Y"; }

            addAttribtoGroup_results.Text = Convert.ToString(agc.InsertRowinAttributeGroupTable(selectedGroup, selectedAttrb, gaValue, agd));
        }

        //Add UserGroup - from Amha's code
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int selectedUser = 0;
            string selectedGroup = "";

            //gets selection from group listbox
            foreach (var item in LB_GROUPS.SelectedItems)
            {
                selectedGroup = ((DataRowView)item)["GroupID"].ToString();
            }

            //gets selection from attribute listbox
            foreach (var item in LB_USER.SelectedItems)
            {
                selectedUser = ((UserClassLIB.UserClass)item).UserID;
            }

            //    int gaValue = Convert.ToInt32(GA_Value_txt.Text);
            string agd = "N";

            if (RB_NO.IsChecked == true)
            { agd = "N"; }
            else
            { agd = "Y"; }

            UserGroupClass aUserGroup = new UserGroupClass();

            aUserGroup.UserID = selectedUser;
            aUserGroup.GroupID = Convert.ToInt32(selectedGroup);
            aUserGroup.Disabled = agd;


            bool isAdded = UserGroupService.AddUserGroup(aUserGroup);

            if (isAdded)
            {
                MessageBox.Show("Your information is successfully added!");
                TBL_Result.Text = "Your information is successfully added!";
            } 
            
           
            

        }
              
      
    }
}
